#include "sport.h"
#include "soft_i2c.h"
#include <stdio.h>
#include <stdlib.h>


// ��ȷ����ʹ�� soft_i2c.c �е� addr ����
extern unsigned char addr;

#ifndef STATUS_REG
#define STATUS_REG 0x27
#endif

#ifndef MOTION_THRESHOLD
#define MOTION_THRESHOLD 1000
#endif

// ȫ�ֱ�������
uint8_t sport_addr = 0x18;


//// �޸���������lic_send_byte �� iic_send_byte
//void iic_send_byte(uint8_t device_addr, uint8_t reg, uint8_t data) {
//    // I2C�����ֽ�ʵ��
//    printf("[I2C] Send to 0x%02X, reg 0x%02X, data 0x%02X\n", addr, reg, data);
//
//}

//// �޸���������lic_read_byte �� iic_read_byte
//uint8_t iic_read_byte(uint8_t device_addr, uint8_t reg, uint8_t* data) {
//    // I2C��ȡ�ֽ�ʵ��
//    printf("[I2C] Read from 0x%02X, reg 0x%02X\n", addr, reg);
//
//
//
//    *data = 0; // ʾ������
//    return 1; // ���سɹ�״̬
//}

void Init_Sensor(void) {
    // ��������ʼ��ʵ��
    printf("[MOTION] Sensor initialized\n");
}

void sport_init(void)
{
    GPIO_InitTypeDef gpio_initStruct = {0};

    gpio_initStruct.Mode = GPIO_MODE_OUTPUT_PP;
    gpio_initStruct.Pin = SPORT_CS_PIN | SPORT_SDO_PIN;
    gpio_initStruct.Speed = GPIO_SPEED_FREQ_LOW;
    gpio_initStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(SPORT_CS_PORT, &gpio_initStruct);
}

void choose_comm_type(Comm_Type commType)
{
    if (IIC == commType)
        HAL_GPIO_WritePin(SPORT_CS_PORT, SPORT_CS_PIN, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(SPORT_CS_PORT, SPORT_CS_PIN, GPIO_PIN_RESET);
}

void choose_addr(unsigned char taddr)
{
    if (0x19 == taddr) {
        HAL_GPIO_WritePin(SPORT_SDO_PORT, SPORT_SDO_PIN, GPIO_PIN_SET);
        sport_addr = 0x19;
				addr = 0x19;
    } else if (0x18 == taddr) {
        HAL_GPIO_WritePin(SPORT_SDO_PORT, SPORT_SDO_PIN, GPIO_PIN_RESET);
        sport_addr = 0x18;
				addr = 0x18;
    }
}

void sport_config(void)
{
    iic_init();
    // 实际配置传感器寄存器
    LIS3DHTR_Init_Sensor();
    HAL_Delay(10);  // 等待传感器稳定
}

// ���������ú���
void LIS3DHTR_Init_Sensor(void)
{
    uint8_t config_data;

    // ����CTRL_REG1: ʹ��XYZ�ᣬ100Hz��������
    config_data = 0x77; // 01010111: XYZʹ��, 100Hz
    iic_sendSingleByte(CTRL_REG1, config_data);

    // ����CTRL_REG4: �߷ֱ���ģʽ
    config_data = 0x98; // 10001000: BDU=1, HR=1
    iic_sendSingleByte (CTRL_REG4, config_data);

    printf("[MOTION] LIS3DHTR sensor configured\n");
}

// �˶���������ȡ����
int16_t LIS3DHTR_ReadAccelX(void)
{
    uint8_t data_low, data_high;
    int16_t accel_x;

    // ��ȡ���ֽ�
    iic_readSingelByte(OUT_X_L, &data_low);
    // ��ȡ���ֽ�
    iic_readSingelByte(OUT_X_H, &data_high);

    accel_x = (data_high << 8) | data_low;
    return accel_x;
}

int16_t LIS3DHTR_ReadAccelY(void)
{
    uint8_t data_low, data_high;
    int16_t accel_y;

    iic_readSingelByte(OUT_Y_L, &data_low);
    iic_readSingelByte(OUT_Y_H, &data_high);

    accel_y = (data_high << 8) | data_low;
    return accel_y;
}

int16_t LIS3DHTR_ReadAccelZ(void)
{
    uint8_t data_low, data_high;
    int16_t accel_z;

    iic_readSingelByte(OUT_Z_L, &data_low);
    iic_readSingelByte(OUT_Z_H, &data_high);

    accel_z = (data_high << 8) | data_low;
    return accel_z;
}

uint8_t LIS3DHTR_ReadMotionStatus(void)
{
    uint8_t status;
    if(iic_readSingelByte(STATUS_REG, &status))printf("iicû��Ӧ��\r\n");
    return status;
}

int LIS3DHTR_CheckMovement(void)
{
    // ����1����ȡ״̬�Ĵ������˶�����־
    uint8_t status = LIS3DHTR_ReadMotionStatus();
//    if (status & 0x40)
//			{
//				printf("[MOTION]  ��⵽�˶� (״̬�Ĵ���:0x%02X)\n", status);
//        return 1;
//			}
//			printf("status: %x\r\n",status);

    // ����2����ȡ���ٶ�ֵ�ж�
		printf("ADDRW : %x , GPIO_STATUS : %d\r\n\r\n",ADDR_W,HAL_GPIO_ReadPin(SPORT_SDO_PORT, SPORT_SDO_PIN));

		while(!(status & 0x08))
		{
			delay_ms(100);
			status = LIS3DHTR_ReadMotionStatus();
		}

    int16_t x = LIS3DHTR_ReadAccelX();
    int16_t y = LIS3DHTR_ReadAccelY();
    int16_t z = LIS3DHTR_ReadAccelZ();

		printf("��ǰ�Ĵ�����ȡֵ X:%x Y:%x Z:%x\n", x, y, z);
    // �����һ����ٶȳ�����ֵ����Ϊ���˶�
    if (abs(x) > MOTION_THRESHOLD || abs(y) > MOTION_THRESHOLD || abs(z) > MOTION_THRESHOLD) {
			printf("[MOTION]  ��⵽�˶� (���ٶ���ֵ) X:%d Y:%d Z:%d\n", x, y, z);
        return 1;
    }
    printf("[MOTION]  ���˶�״̬\n");
    return 0;
}
